<?php
class HotelPackageController extends CBaseController
{		
	public function __construct($config=null) 
    { 
      parent::__construct($config);	  
    } 
	
	public function init($tpl)
	{
		parent::init($tpl);
		$this->reqLogin();
		$this->templator->addViewsDir(dirname(__FILE__).'/../views');
		$this->templator->assign('menu','package');
	}
	
	public function assign()
	{
		$id = $_POST['id'];
		$package_ids = $_POST['package_ids'];
		$hotel = R::load('hotel',$id);		
		foreach($package_ids as $package_id)
		{
			$package = R::load('travelpackage',$package_id);
			R::associate( $hotel, $package );
		}
		$this->packages($id);
	}
	
	public function unassign()
	{
		$id = $_GET['id'];
		$hotel = R::load('hotel',$id);
		$package_id = $_GET['package_id'];
		$package = R::load('travelpackage',$package_id);
		R::unassociate( $hotel, $package );
		$this->packages();
	}
	
	public function packages($id=null)
	{
		if(!isset($id))
		$id = $_GET['id'];
		$hotel = R::load('hotel',$id);
		$assignedPackages= R::related( $hotel, 'travelpackage' );
		$this->templator->assign('hotel',$hotel);		
		$this->templator->assign('packages',array_merge($assignedPackages, array()));
		$this->templator->display('package/hotel_package.html');
	}
	
	public function select()
	{
		$id = $_POST['id'];
		$hotel = R::load('hotel',$id);
		$assignedPackages= R::related( $hotel, 'travelpackage' );		
		$allPackages = R::findAll('travelpackage');
		$packages =array_diff($allPackages, $assignedPackages);
		$this->templator->assign('hotel',$hotel);
		$this->templator->assign('packages',array_merge($packages, array()));
		$this->templator->display('package/package_list.html');
	}
	
}	